<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Order extends ShopBackEndController {
	public function __construct()
    {
        parent::__construct();
        $this->load->model('repo/shop_order_model', 'shop_order_model');
        $this->load->library('mypaging');
    }

    private function intialPaging()
    {
        $this->mypaging->setCount($this->shop_order_model->countOrder($this->shop->getId()));
        $this->mypaging->setLimit(ORDER_LIMIT);
        $this->mypaging->setMainURL(mns_order_paging_url());
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function index($pageIndex = 1)
    {
        $this->intialPaging();
        $pagingBar = $this->mypaging->getBar($pageIndex);
        $offset = ($pageIndex - 1) * ORDER_LIMIT;
        $aOrder = $this->shop_order_model->paging($this->shop->getId(), ORDER_LIMIT, $offset);
        $aReturn = array(
            'pageTitle' => 'Hóa đơn',
            'aOrder' => $aOrder,
            'pagingBar' => $pagingBar);
        $this->renderView('backend/shop/order/index', $aReturn);
    }

    public function get($id){
        $aOrderDetail = $this->shop_order_model->get($this->shop->getId(), $id);
        if($aOrderDetail==null){
            show_404();
        }
        $aReturn = array(
            'pageTitle' => 'Hoá đơn #' . $id,
            'shop_info' => $this->shop,
            'aOrderDetail' => $aOrderDetail);
        $this->load->view('backend/shop/order/single.html.php', $aReturn);
    }

    private function intialSearchPaging($search_key)
    {
        $this->mypaging->setCount($this->shop_order_model->countSearchOrder($this->shop->getId(), $search_key));
        $this->mypaging->setLimit(ORDER_LIMIT);
        $this->mypaging->setMainURL(mns_order_search_url());
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function search($pageIndex=1){
        $search_key = $this->input->get('s', true);
        $this->intialSearchPaging($search_key);
        $pagingBar = $this->mypaging->getBar($pageIndex);
        $offset = ($pageIndex - 1) * ORDER_LIMIT;
        $aOrder = $this->shop_order_model->searchPaging($this->shop->getId(), $search_key, ORDER_LIMIT, $offset);
        $aReturn = array(
            'pageTitle' => 'Tìm kiếm hóa đơn',
            'aOrder' => $aOrder,
            'searchkey' => $search_key,
            'pagingBar' => $pagingBar);
        $this->renderView('backend/shop/order/index', $aReturn);
    }
}
?>